package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.controller.vo.Result;
import com.example.entity.User;
import com.example.service.CommentLikeService;
import com.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/commentlike")
public class CommentLikeController {

    @Autowired
    private CommentLikeService commentLikeService;
    @Autowired
    private IUserService iuserService;
    // 点赞/取消点赞切换接口
    @PostMapping("/toggle")
    public Result toggleLike(
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "userName") String userName) { // 实际项目中从登录信息获取userId
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account",userName);
        User user = iuserService.getOne(queryWrapper);
        Integer userId=user.getId();
        return Result.success(commentLikeService.toggleLike(id, userId));
    }
    // 查询点赞状态接口 ,判断当前用户是否对该评论点赞,如果点赞返回true,如果未点赞返回false
    @GetMapping("/status")
    public Boolean getLikedStatus(
            @RequestParam(value = "commentId") Integer commentId,
            @RequestParam(value = "userName") String userName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account",userName);
        User user = iuserService.getOne(queryWrapper);
        Integer userId=user.getId();
        Result result = commentLikeService.getLikedStatus(commentId, userId);
        Boolean data = (Boolean) result.getData();
        return data;
    }
}
